package mvp.com.bbc.rcjavaretrofitdaggermvp.app.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import java.util.ArrayList;

/**
 * 活动管理工具
 *
 * @author LianDaYu
 * @date 2016年6月13日上午11:06:32
 */
public class ActivityUtils {
    // 声明一个集合用于记录所有打开的活动
    public static ArrayList<Activity> list = new ArrayList<Activity>();

    // 加入活动对象------>onCreate
    public static void add(Activity activity) {
        list.add(activity);
    }

    // 移除一个具体的活动--->onDestroy
    public static void remove(Activity activity) {
        list.remove(activity);
    }

    // 返回上一个页面
    public static void break_off() {
        list.get(list.size() - 1).finish();
    }

    // 关闭所有存在的活动---->close
    public static void removeAll() {
        for (Activity activity : list) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }

    // 关闭所有存在的活动除了自己---->close
    public static void removeAllExceptThis(Activity a) {
        for (Activity activity : list) {
            if (!activity.isFinishing() && activity != a) {
                activity.finish();
            }
        }
    }

    /**
     * FragmentActivity跳转
     *
     * @param cla  需要跳转到的页面
     * @param page 设置默认选中的Fragment
     */
    public static void skipFragmentActivity(Class<?> cla, int page) {
        Context context = ActivityUtils.list.get(ActivityUtils.list.size() - 1);
        Intent intent = new Intent(context, cla);
        //用Bundle携带数据
        Bundle bundle = new Bundle();
        bundle.putInt("page", page);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    /**
     * Web详情页跳转
     *
     * @param cla
     * @param url
     */
    public static void skipWebActivity(Class<?> cla, String url) {
        Context context = ActivityUtils.list.get(ActivityUtils.list.size() - 1);
        Intent intent = new Intent(context, cla);
        //用Bundle携带数据
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void skipActivity(Class<?> cla, Bundle bundle) {
        Context context = ActivityUtils.list.get(ActivityUtils.list.size() - 1);
        Intent intent = new Intent(context, cla);
        if (bundle != null)
            intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void skipActivityFinish(Class<?> cla, Bundle bundle) {
        Context context = ActivityUtils.list.get(ActivityUtils.list.size() - 1);
        Intent intent = new Intent(context, cla);
        if (bundle != null)
            intent.putExtras(bundle);
        context.startActivity(intent);
        list.get(list.size() - 1).finish();
    }

    public static void skipActivityForResult(Activity activity, Class<?> cla, Bundle bundle, int requestCode) {
        Intent intent = new Intent(activity, cla);
        if (bundle != null)
            intent.putExtras(bundle);
        activity.startActivityForResult(intent, requestCode);
    }
}
